﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.BeamNG.Forms;
using DriveBeamEditor.Controls.General;
using JBeamLibrary;
using JBeamLibrary.VehicleParts;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.Buttons
{
    public partial class NodeSelectionButton : XButton
    {
        #region Fields

        private VehiclePart _vehiclePart;

        public event EventHandler VehiclePartChanged;
        public event EventHandler<Node> SelectedNodeChanged;

        #endregion

        #region Properties

        public Node SelectedNode
        {
            get { return pcNodeSelection.SelectedNode; }
            set { pcNodeSelection.SelectedNode = value; }
        }

        public VehiclePart VehiclePart
        {
            get { return _vehiclePart; }
            set
            {
                if (_vehiclePart == value) return;
                _vehiclePart = value;
                OnVehiclePartChanged();
            }
        }

        #endregion

        #region CTORs

        public NodeSelectionButton()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (Config.SelectNodesFromFromCurrentPartOnly && !pddNodeSelection.Visible)
                pddNodeSelection.Show(this);
            else if (!Config.SelectNodesFromFromCurrentPartOnly && !pddNodeSelectionFull.Visible)
                pddNodeSelectionFull.Show(this);
        }

        private void pcNodeSelection_SelectedNodeChanged(object sender, Node e)
        {
            if (SelectedNodeChanged != null)
                SelectedNodeChanged(this, e);
        }

        #endregion

        #region Methods

        private void OnVehiclePartChanged()
        {
            pcNodeSelection.VehiclePart = VehiclePart;
            pcNodeSelectionFull.Vehicle = VehiclePart != null ? VehiclePart.File.Parent : null;

            if (VehiclePartChanged != null)
                VehiclePartChanged(this, new EventArgs());
        }

        #endregion


    }
}
